package Lab1;

import java.util.Scanner;
import java.util.Random;

public class ArrayExercises {

    public static void reverseInPlace(int[] array) {
        int temp;
        for (int i = 0; i < array.length / 2; i++) {
            temp = array[i];
            array[i] = array[array.length - 1 - i];
            array[array.length - 1 - i] = temp;
        }
        System.out.print("Reversed: ");
        printArray(array);
    }

    public static void printArray(int[] array) {
        for (int n : array) {
            System.out.print(n + ", ");
        }
        System.out.println();
    }

    public static int[] testArray() {
        Scanner scan = new Scanner(System.in);
        System.out.print("Type array length: ");
        int n = scan.nextInt();
        int[] array = new int[n];
        scan.nextLine();

        String[] nums;
        while (true) {
            System.out.print("Type " + n + " numbers, split by \",\":\n");
            String line = scan.nextLine();
            line = line.replaceAll("\\s", "");
            nums = line.split(",");
            if (nums.length != n) {
                System.out.print("There is supposed to have " + n + " numbers, got " + nums.length + " instead!\n");
            } else {
                break;
            }
        }

        for (int i = 0; i < n; i++) {
            array[i] = Integer.parseInt(nums[i]);
        }

        return array;
    }

    public static int[] randomArray(int n) {
        int[] l = new int[n];
        Random r = new Random();
        for (int i = 0; i < n; i++) {
            l[i] = r.nextInt();
        }
        return l;
    }

    /**
     * A selectionSort method for sorting numbers
     */
    public static void selectionSort(int[] list) {
        for (int i = 0; i < list.length; i++) {
// Find the minimum in the list[i..list.length-1]
            int currentMin = list[i];
            int currentMinIndex = i;
            for (int j = i + 1; j < list.length; j++) {
                if (currentMin > list[j]) {
                    currentMin = list[j];
                    currentMinIndex = j;
                }
            }
// Swap list[i] with list[currentMinIndex] if necessary;
            if (currentMinIndex != i) {
                list[currentMinIndex] = list[i];
                list[i] = currentMin;
            }
        }
//        printArray(list);
    }

    public static boolean isConsecutiveFour(int[] values) {
        int n = 0;
        int temp = values[0];
        for (int a : values) {
            if (a == temp) {
                n++;
            } else {
                temp = a;
                n = 1;
            }
            if (n == 4) {
                System.out.println("Is four-consecutive");
                return true;
            }
        }
        System.out.println("Not four-consecutive");
        return false;
    }

    public static void main(String[] agrs) {

        int[] a = {0, 1, 2, 3, 4};
        printArray(a);
        

        reverseInPlace(a);
        isConsecutiveFour(a);

        System.out.println();

        int[] b = testArray();

        isConsecutiveFour(b);
        reverseInPlace(b);
        selectionSort(b);

        int[] c = randomArray(6);
        printArray(c);
    }
}
